package com.levi.product.controller;

import com.levi.api.controller.order.OrderControllerApi;
import com.levi.api.controller.product.ProductControllerApi;
import com.levi.pojo.Product;
import com.levi.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.annotation.RequestScope;

@RestController
@Slf4j
@RequestScope
public class ProductController implements ProductControllerApi {

    @Value("${config.info}")
    private String info;

    @Autowired
    private ProductService productService;

    @Autowired
    private OrderControllerApi orderControllerApi;
    @Override
    public Boolean save() {
        Product product = new Product();
        product.setId(1);
        product.setName("appler");
        product.setStock(100);
        productService.save(product);
        log.info("保存商品成功");
        return true;
    }

    @Override
    public String decutProduct(Integer id, Integer count) {
        Product product = productService.getById(id);
        Integer stock = product.getStock();
        if (stock >= count) {
            product.setStock(stock - count);
            productService.updateById(product);
            log.info("扣减库存成功,开始下单....");
            Boolean order = orderControllerApi.createOrder(id, count);
        }
        return "success";

    }

    @Override
    public String configInfo() {
        return info;
    }

    @Override
    public String gateway() {
        return "gateway   success";
    }
}
